<template>
  <el-row :gutter="24" class="pl80">
    <el-col>
      <h2>
        执业资格信息
        <el-button size="small" type="primary" @click="handleEdit(false, {})" icon="el-icon-plus" />
      </h2>
    </el-col>
    <el-col>
      <e-table
        style="width: 98%"
        @getList="handleGetList"
        :tableData="tableData"
        :columns="columns"
        :pageObj="null"
      >
        <el-table-column label="附件图片" slot="photo" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.savePath && scope.row.savePath.length > 0">
              <el-button @click="handlePreview" type="text">{{ scope.row.savePath.length }}张</el-button>
            </template>
            <template v-else>无</template>
          </template>
        </el-table-column>
        <el-table-column slot="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>修改</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </e-table>
    </el-col>
    <qualification-modal @refresh="handleRefresh" ref="qualificationModal" />
    <preview-modal ref="previewModal" />
  </el-row>
</template>

<script>
import { setSession, getSession } from '@/utils/util'
import { getQualificationAll, deleteQualification } from '@/api/archives'
import QualificationModal from './QualificationModal'
import PreviewModal from '@/components/PreviewModal'
import { openNotification } from '@/utils/util'

export default {
  components: {
    QualificationModal,
    PreviewModal
  },
  data() {
    return {
      tableData: [],
      columns: [
        {
          label: '资格名称',
          prop: 'qualificationName'
        },
        {
          label: '等级',
          prop: 'qualificationLevel'
        },
        {
          label: '证书取得时间',
          prop: 'qualificationStart'
        },
        {
          label: '证书有效期限',
          prop: 'qualificationEnd'
        },
        { slot: 'photo' },
        {
          label: '备注',
          prop: 'remark'
        },
        {
          slot: 'action'
        }
      ],
      queryParam: {},
      userRow: null
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
    this.handleGetList({ userId: this.userRow.userId })
  },
  methods: {
    handleRefresh() {
      this.handleGetList({ userId: this.userRow.userId })
    },
    handlePreview(row) {
      this.$refs.previewModal.show(row.savePath)
    },
    handleEdit(bool, obj) {
      this.$nextTick(() => {
        this.$refs.qualificationModal.edit(bool, obj)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteQualification(row.userQualificationId, {})
          if (res.state === '000000') {
            openNotification('success', '删除成功')
            _this.handleGetList({ userId: _this.userRow.userId })
          }
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getQualificationAll(this.queryParam)
      console.log('reward res', res)

      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
        // this.total = total
      } else {
        this.tableData = []
        // this.total = 1
      }
      this.loading = false
    }
  }
}
</script>
